﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Sys.Interfaces;
using AMS.DataAccess.Sys.BO;
using AMS.DataAccess.Sys.Filters;

public partial class User_AllUsers : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DoDataBind();
        }
    }

    #region Data Binding
    protected string FormatDateTime(object value)
    {
        return Utils.FormateDateTime(value, false);
    }

    private void DoDataBind()
    {
        int total = 0;
        IUserManager userMgr = ManagerFactory.CreateUserManager();
        UserFilter filter = ucSearch.GetUserFilter();
        List<UserInfo> users = userMgr.GetPagedUsers(CallContext, filter, pager.CurrentPageIndex, pager.PageSize, out total);
        gv.DataSource = users;
        gv.DataKeyNames = new string[] { "AID" };
        gv.DataBind();

        lblTotal.Text = total.ToString();
        if (filter.IsFiltered())
        {
            lblFiltered.Text = "(已筛选)";
        }
        else
        {
            lblFiltered.Text = "";
        }
    }

    protected string FormatDateTime(object value, bool showTime)
    {
        return Utils.FormateDateTime(value, showTime);
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        DoDataBind();
    }
    protected void pager_PageChanged(object sender, EventArgs e)
    {
        DoDataBind();
    }

    protected void pager2_PageChanged(object sender, EventArgs e)
    {
        DoDataBind();
    }
    protected void ucSearch_OnFilterChanged(object sender, EventArgs e)
    {
        DoDataBind();
    }
    #endregion

    List<int> GetCheckItemIDs()
    {
        List<int> ids = new List<int>();
        foreach (GridViewRow row in gv.Rows)
        {
            CheckBox chk = row.FindControl("chk") as CheckBox;
            if (chk.Checked)
            {
                int id = Convert.ToInt32(gv.DataKeys[row.RowIndex].Value);
                ids.Add(id);
            }
        }

        return ids;
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        List<int> userIDs = GetCheckItemIDs();
        if(userIDs.Count == 0)
        {
            return;
        }

        IUserManager userMgr = ManagerFactory.CreateUserManager();
        int count = userMgr.BatchSetUsersInactive(CallContext, userIDs);
        DoDataBind();
        if (count == userIDs.Count)
        {
            MsgInfo("成功：" + count.ToString());
        }
        else
        {
            MsgWarning(string.Format("成功：{0}，失败：{1}。失败原因：用户只读或可能被删除", count, userIDs.Count - count));
        }
    }

}
